package linklist;

public class LinkedList<E> {

    private class Node {
        private E e;
        private Node next;

        private Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        private Node(E e) {
            this(e, null);
        }

        private Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node head;
    private int size;

    public LinkedList() {
        head = new Node(null, null);
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void addFirst(E e) {
        add(0, e);
    }

    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is illegal");
        }

        Node pre = head;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }

        pre.next = new Node(e, pre.next);
        size++;

    }

    public void addLast(E e) {
        add(size, e);
    }

    public E get(int index) {
        if (index < 0 || index >= 0) {
            throw new IllegalArgumentException("index is illegal");
        }
        Node cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        return cur.e;

    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size);
    }

    public void set(int index, E e) {
        if (index <= 0 || index > size) {
            throw new IllegalArgumentException("index is illegal");
        }

        Node cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        cur.e = e;
    }

    public boolean contains(E e) {
        Node cur = head;
        for (int i = 0; i < size; i++) {
            cur = cur.next;
            if (cur.e == e) {
                return true;
            }

        }

        return false;
    }

    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index is illegal");
        }
        Node pre = head;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }

        Node resNode = pre.next;
        pre.next = resNode.next;
        resNode.next = null;
        size--;
        return resNode.e;

    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("链表的大小：" + size + "\n");
        Node cur = head;
        for (int i = 0; i < size; i++) {
            cur = cur.next;
            stringBuilder.append(cur.e);
            if (i != size - 1) {
                stringBuilder.append("->");
            }
        }

        return stringBuilder.toString();
    }


}
